﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.CommandLineUtilities
{
    public class CommandLineWorker:IWorker
    {
        #region fields
        private Thread _WorkerThread;
        private CommandLineInstruction _Instruction;
        private CommandLineOutput _Output;
        private RunContext _Context;
        private Guid _TicketID;
        private string _WorkflowName;
        private string _ComponentName;

        /// <summary>
        /// 
        /// </summary>
        public string ComponentName
        {
            get { return _ComponentName; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string WorkflowName
        {
            get { return _WorkflowName; }
        }

        public Thread WorkerThread
        {
            get { return _WorkerThread; }
        }

        public InstructionBase Instruction
        {
            get { return _Instruction; }
        }

        public ResultBase Output
        {
            get { return _Output; }
        }

        public Type ComponentType
        {
            get { return typeof(CommandLineComponent); }
        }

        public RunContext Context
        {
            get { return _Context; }
            set { _Context = value; }
        }
        #endregion

        public void SetInstruction(Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._TicketID = ticketGuid;
            this._Instruction = (CommandLineInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        public void SetInstruction(RunContext context, Guid ticketGuid, InstructionBase request, string workflowName, string componentName)
        {
            this._Context = context;
            this._TicketID = ticketGuid;
            this._Instruction = (CommandLineInstruction) request;
            this._WorkflowName = workflowName;
            this._ComponentName = componentName;
        }

        public void DoWork()
        {
            if(this.WorkerStartedEvent !=null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);
            }
            this._WorkerThread = Thread.CurrentThread;

            this.CallExe();
        }

        public void DoWorkAsync()
        {
            if (this.WorkerStartedEvent != null)
            {
                this.WorkerStartedEvent(this._TicketID, this, this.GetType(), this._Instruction);
            }

            this._WorkerThread=new Thread(new ThreadStart(this.CallExe));
            this._WorkerThread.Start();
        }

        public void CancelWork()
        {
            if(this._WorkerThread !=null && this._WorkerThread.IsAlive)
            {
                this._WorkerThread.Abort();
                this._WorkerThread.Join();

                if (this.WorkerCancelledEvent != null)
                    this.WorkerCancelledEvent(this._TicketID, this.GetType(), this._Instruction);
            }
        }

        public event WorkerStarted WorkerStartedEvent;
        public event WorkerFinished WorkerFinishedEvent;
        public event WorkerHasError WorkerErredEvent;
        public event WorkerCancelled WorkerCancelledEvent;
        public event WorkerStatusChanged WorkerStatusChangedEvent;

        #region work
        private void CallExe()
        {
            try
            {
                Process proc = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = this._Instruction.ProgramFilePath;
                startInfo.Arguments = this._Instruction.ArgTemplate;
                proc.StartInfo = startInfo;
                proc.Start();
                proc.WaitForExit();

                this._Output = new CommandLineOutput(this._Instruction.OwnerComponentID, this._TicketID, null);

                if (this.WorkerFinishedEvent != null)
                {
                    this.WorkerFinishedEvent(this._TicketID, this.GetType(), this._Instruction, this._Output);
                }
            }
            catch (Exception ex)
            {
                if (this.WorkerErredEvent != null)
                {
                    this.WorkerErredEvent(this._TicketID, this.GetType(), this._Instruction, ex.ToString(),true);
                }
            }
        }
        #endregion
    }
}
